package com.siruier.boss.ui.fragment.order

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.siruier.boss.R
import com.siruier.boss.bean.GiftOrderDetailBean
import com.siruier.boss.databinding.FragmentGiftWarehouseListBinding
import com.siruier.boss.databinding.ItemGiftWarehouseBinding
import com.siruier.boss.ui.activity.mall.UserOrderDetailsActivity
import com.siruier.boss.ui.base.BaseFragment
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.ext.clipboard
import com.siruier.boss.ui.ext.showLoadingBeforeSuccessCallback
import com.siruier.boss.ui.ext.textSpan
import com.siruier.boss.ui.ext.toColor
import com.siruier.boss.ui.helper.RefreshHelper
import com.siruier.boss.ui.utils.GlideUtils


open class GiftWarehouseListFragment<T : FragmentGiftWarehouseListBinding> : BaseFragment<T>() {

    protected val mGiftWarehouseAdapter = GiftWarehouseAdapter()
    protected lateinit var mRefreshHelper: RefreshHelper<GiftOrderDetailBean>

    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        super.initView(rootView, savedInstanceState)
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.rvOrder, mGiftWarehouseAdapter, listener = ::onReload)
        vb.refreshLayout.setPrimaryColorsId(R.color.main_bg, R.color.text_color)
        mRefreshHelper.onRefresh()
    }

    open fun onReload() {

    }

    open fun onBindViewHolder(vb: ItemGiftWarehouseBinding, bean: GiftOrderDetailBean, position: Int) {

    }

    inner class GiftWarehouseAdapter : BaseRecyclerAdapter<ItemGiftWarehouseBinding, GiftOrderDetailBean>() {

        override fun onBindViewHolder(vb: ItemGiftWarehouseBinding, bean: GiftOrderDetailBean, position: Int) {
            // 目前只能购买一个商品
            bean.goodsList.takeIf { it.size > 0 }?.get(0)?.apply {
                GlideUtils.intoView(vb.ivImage.context, picUrl, vb.ivImage)
                vb.tvNumber.text = "x${number}"
                vb.tvGoodsName.text = goodsName
                vb.tvGoodsMoney.textSpan("市场价${priceDesc}") {
                    spanText = priceDesc
                    addForegroundColorSpan(R.color.main_color.toColor())
                }
            }
            this@GiftWarehouseListFragment.onBindViewHolder(vb, bean, position)
            vb.buttonCopySn.isVisible = true
            setViewClick(position, vb.root, vb.buttonCopySn)
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            super.subOnClickAdapter(v, position)
            if (R.id.root_view == v.id) {
                launchStartActivityForResult(UserOrderDetailsActivity::class.java, options = {
                    putString("mOrderSn", mGiftWarehouseAdapter.getItem(position).orderSn)
                }) {
                    onReload()
                }
            } else if (R.id.button_copy_sn == v.id) {
                getItem(position).orderSn.clipboard()
            }
        }
    }
}
